<!DOCTYPE html> <html><!--
 Archive processed by SingleFile 
 url: https://mp.weixin.qq.com/s/3LA6RwfenjHWNUlZYQQOkg 
 saved date: Thu Nov 21 2019 10:27:08 GMT+0800 (中国标准时间) 
--><meta charset=utf-8>
<meta http-equiv=X-UA-Compatible content="IE=edge">
<meta name=viewport content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0,viewport-fit=cover">
<meta name=apple-mobile-web-app-capable content=yes>
<meta name=apple-mobile-web-app-status-bar-style content=black>
<meta name=format-detection content="telephone=no">
<meta name=description content=性能问题的大部分原因并不在于JAVA语言，而是程序本身。养成良好的编码习惯非常重要，能够显著地提升程序性能。>
<meta name=author content>
<meta property=og:title content="Java 性能优化的 45 个细节">
<meta property=og:url content="http://mp.weixin.qq.com/s?__biz=MzI3NjU2ODA5Mg==&amp;mid=2247484497&amp;idx=3&amp;sn=91dd735dc61b16948f352c3886be5469&amp;chksm=eb72c522dc054c343926a78157e141483b3a24cc2508a56b1d5ff6c6d8f625e23b9f7dfb8b18#rd">
<meta property=og:image content="http://mmbiz.qpic.cn/mmbiz_jpg/M7B64fHXISuhVVYBCelMeRG0maaibVOx84GFKibZjmVNW9BaAmRILOibJ0peibPq66ibRWTuibr03gkWOa2dbpam8syA/0?wx_fmt=jpeg">
<meta property=og:description content=性能问题的大部分原因并不在于JAVA语言，而是程序本身。养成良好的编码习惯非常重要，能够显著地提升程序性能。>
<meta property=og:site_name content=微信公众平台>
<meta property=og:type content=article>
<meta property=og:article:author content>
<meta property=twitter:card content=summary>
<meta property=twitter:image content="http://mmbiz.qpic.cn/mmbiz_jpg/M7B64fHXISuhVVYBCelMeRG0maaibVOx84GFKibZjmVNW9BaAmRILOibJ0peibPq66ibRWTuibr03gkWOa2dbpam8syA/0?wx_fmt=jpeg">
<meta property=twitter:title content="Java 性能优化的 45 个细节">
<meta property=twitter:creator content>
<meta property=twitter:site content=微信公众平台>
<meta property=twitter:description content=性能问题的大部分原因并不在于JAVA语言，而是程序本身。养成良好的编码习惯非常重要，能够显著地提升程序性能。>
<title>Java 性能优化的 45 个细节</title>
<style media>.rich_media_inner{overflow-wrap:break-word;hyphens:auto}.rich_media_area_primary{padding:calc(20px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) 12px calc(16px + env(safe-area-inset-left))}.rich_media_area_extra{padding:0 calc(16px + env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left))}html{line-height:1.6}body{color:#333;background-color:#f2f2f2;letter-spacing:.034em}h2{font-weight:400}*{margin:0;padding:0}a{-webkit-tap-highlight-color:rgba(0,0,0,0)}.rich_media_title{font-size:22px;line-height:1.4;margin-bottom:14px}@supports(-webkit-overflow-scrolling:touch){.rich_media_title{font-weight:700}}.rich_media_meta_list{margin-bottom:22px;line-height:20px;font-size:0;overflow-wrap:break-word;word-break:break-all}.rich_media_meta_list em{font-style:normal}.rich_media_meta{display:inline-block;vertical-align:middle;margin:0 10px 10px 0;font-size:15px;-webkit-tap-highlight-color:rgba(0,0,0,0)}.rich_media_meta_text{color:rgba(0,0,0,0.3)}.rich_media_meta_nickname{position:relative}.rich_media_content{overflow:hidden;color:#333;font-size:17px;overflow-wrap:break-word;hyphens:auto;text-align:justify;z-index:0}.rich_media_content *{max-width:100% !important;box-sizing:border-box !important;overflow-wrap:break-word !important}.rich_media_content p{clear:both;min-height:1em}.rich_media_content .list-paddingleft-2{padding-left:2.2em}@media screen and (min-width:1024px){.rich_media_area_primary_inner,.rich_media_area_extra_inner{max-width:677px;margin-left:auto;margin-right:auto}.rich_media_area_primary{padding-top:32px}}.appmsg_skin_default .rich_media_area_primary{background-color:#fff}.appmsg_style_default .rich_media_tool{padding-top:15px}.read-more__area{margin:30px 0}html,body{height:100%}</style>
<!--[if lt IE 9]>
<link rel="stylesheet" type="text/css" href="//res.wx.qq.com/mmbizwap/zh_CN/htmledition/style/page/appmsg_new/pc492bcc.css">
<![endif]-->
<style id=page/appmsg_new/not_in_mm.css>.weui-flex{display:flex}.weui-flex__item{-webkit-box-flex:1;flex:1 1 0%}html{text-size-adjust:100%}body{line-height:1.6;font-family:-apple-system-font,BlinkMacSystemFont,"Helvetica Neue","PingFang SC","Hiragino Sans GB","Microsoft YaHei UI","Microsoft YaHei",Arial,sans-serif;font-size:16px}body,h2,p,ul,ol{margin:0}a{color:#576b95;text-decoration:none}body,html{-webkit-appearance:none;-webkit-tap-highlight-color:rgba(0,0,0,0)}@media(orientation:portrait){@-webkit-keyframes opr_fade_out{0%{opacity:1}100%{opacity:0}}@-webkit-keyframes opr_fade_in{0%{opacity:0}100%{bottom:0;opacity:1}}}@-webkit-keyframes opr_fade_out{0%{opacity:1}100%{opacity:0}}@-webkit-keyframes opr_fade_in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes opacity-60-25-0-12{0%{opacity:.25}0.01%{opacity:.25}0.02%{opacity:1}60.01%{opacity:.25}100%{opacity:.25}}@-webkit-keyframes opacity-60-25-1-12{0%{opacity:.25}8.34333%{opacity:.25}8.35333%{opacity:1}68.3433%{opacity:.25}100%{opacity:.25}}@-webkit-keyframes opacity-60-25-2-12{0%{opacity:.25}16.6767%{opacity:.25}16.6867%{opacity:1}76.6767%{opacity:.25}100%{opacity:.25}}@-webkit-keyframes opacity-60-25-3-12{0%{opacity:.25}25.01%{opacity:.25}25.02%{opacity:1}85.01%{opacity:.25}100%{opacity:.25}}@-webkit-keyframes opacity-60-25-4-12{0%{opacity:.25}33.3433%{opacity:.25}33.3533%{opacity:1}93.3433%{opacity:.25}100%{opacity:.25}}@-webkit-keyframes opacity-60-25-5-12{0%{opacity:.270958}41.6767%{opacity:.25}41.6867%{opacity:1}1.67667%{opacity:.25}100%{opacity:.270958}}@-webkit-keyframes opacity-60-25-6-12{0%{opacity:.375125}50.01%{opacity:.25}50.02%{opacity:1}10.01%{opacity:.25}100%{opacity:.375125}}@-webkit-keyframes opacity-60-25-7-12{0%{opacity:.479292}58.3433%{opacity:.25}58.3533%{opacity:1}18.3433%{opacity:.25}100%{opacity:.479292}}@-webkit-keyframes opacity-60-25-8-12{0%{opacity:.583458}66.6767%{opacity:.25}66.6867%{opacity:1}26.6767%{opacity:.25}100%{opacity:.583458}}@-webkit-keyframes opacity-60-25-9-12{0%{opacity:.687625}75.01%{opacity:.25}75.02%{opacity:1}35.01%{opacity:.25}100%{opacity:.687625}}@-webkit-keyframes opacity-60-25-10-12{0%{opacity:.791792}83.3433%{opacity:.25}83.3533%{opacity:1}43.3433%{opacity:.25}100%{opacity:.791792}}@-webkit-keyframes opacity-60-25-11-12{0%{opacity:.895958}91.6767%{opacity:.25}91.6867%{opacity:1}51.6767%{opacity:.25}100%{opacity:.895958}}@-webkit-keyframes loading{0%{transform:rotate3d(0,0,1,0)}100%{transform:rotate3d(0,0,1,360deg)}}@keyframes loading{0%{transform:rotate3d(0,0,1,0)}100%{transform:rotate3d(0,0,1,360deg)}}.article_extend_area{padding:30px 0 0}.article_extend_area:empty{display:none}@supports(-webkit-overflow-scrolling:touch){.reward_button{font-weight:700}}.rich_media_extra{position:relative}.top_banner{background-color:#fff}.ct_mpda_wrp{margin:38px 0 20px}.like_btn{-webkit-appearance:none;-webkit-tap-highlight-color:rgba(0,0,0,0);outline:0;background-color:transparent;border:0;display:inline-block;vertical-align:middle;padding:0;font-size:15px;font-family:inherit;line-height:2.13333;color:#576b95}.like_btn::before{font-size:16px;content:"";display:inline-block;width:1em;height:1.125em;vertical-align:middle;margin-top:-0.25em;margin-right:.05em;background:url("data:image/svg+xml;charset=utf8, %3Csvg width='18' height='20' viewBox='0 0 18 20' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M5.485 3.785l2.506-2.477a1.674 1.674 0 0 1 2.352 0l2.505 2.477 3.423.908a1.653 1.653 0 0 1 1.18 2.026l-.917 3.382.918 3.384a1.652 1.652 0 0 1-1.18 2.024l-3.399.902-2.53 2.493a1.674 1.674 0 0 1-2.352 0l-2.506-2.477-3.423-.908a1.653 1.653 0 0 1-1.18-2.026l.917-3.383-.918-3.392a1.652 1.652 0 0 1 1.18-2.025l3.424-.908zm.836 1.447l.006 2.298c0 .59-.317 1.138-.828 1.438l-2.015 1.143 2.005 1.136c.517.29.838.841.838 1.435l-.006 2.298 2.01-1.156a1.667 1.667 0 0 1 1.675-.003l2.007 1.154-.007-2.302c0-.583.319-1.13.829-1.43l2.014-1.142-2.005-1.136a1.647 1.647 0 0 1-.838-1.435l.007-2.298-2.01 1.156a1.65 1.65 0 0 1-1.67.001L6.321 5.232zm-1.094 2.3L5.22 4.994l-2.878.763a.552.552 0 0 0-.398.674l.77 2.851 2.23-1.264a.573.573 0 0 0 .283-.486zm-.278 4.673L2.714 10.94l-.77 2.84a.553.553 0 0 0 .399.676l2.877.763.007-2.537a.548.548 0 0 0-.278-.476zm3.935 2.57l-2.216 1.274 2.096 2.073c.222.22.583.22.806 0l2.103-2.073-2.214-1.274a.57.57 0 0 0-.575 0zm4.222-2.104l.007 2.538 2.879-.763a.552.552 0 0 0 .398-.674l-.771-2.843-2.23 1.265a.57.57 0 0 0-.283.477zm.279-4.664l2.234 1.266.77-2.84a.553.553 0 0 0-.399-.676l-2.877-.763-.007 2.537c0 .196.107.38.279.476zm-4.501-2.57c.176.104.39.104.566 0l2.215-1.274L9.57 2.09a.574.574 0 0 0-.805 0L6.668 4.163l2.216 1.274z' fill='%23576B95' fill-rule='evenodd'/%3E%3C/svg%3E") 0 0 / 1em no-repeat transparent}.like_num{font-size:15px;margin-left:.2em}.like_comment_wrp{font-size:17px;margin-top:9px;margin-bottom:8px;position:relative;z-index:1}.like_comment_wrp::before,.like_comment_wrp::after{content:"";display:inline-block;width:0;height:0;border-width:0 7px 7px;border-style:dashed dashed solid;border-color:transparent transparent rgba(0,0,0,0.03);position:absolute;top:-7px;right:28px}.like_comment_wrp::after{border-bottom-color:#f7f7f7;top:-6px}.like_comment_inner{background-color:rgba(0,0,0,0.03);border-radius:4px;overflow:hidden;padding:24px 16px;display:flex;-webkit-box-align:center;align-items:center;text-align:center}.like_comment_primary_wrp{font-size:16px;margin-top:9px;margin-bottom:4px;background-color:#fff;z-index:21}.like_comment_primary_wrp::before,.like_comment_primary_wrp::after{content:"";display:inline-block;width:0;height:0;border-width:0 7px 7px;border-style:dashed dashed solid;border-color:transparent transparent #fff;position:absolute;top:-7px;right:28px}.like_comment_primary_wrp::after{border-bottom-color:#fff;top:-6px}.like_comment_primary_wrp.editing{position:fixed;top:10px;bottom:0;left:0;right:0;margin:0}.like_comment_primary_wrp.editing::before,.like_comment_primary_wrp.editing::after{display:none}.like_comment_primary_mask{position:fixed;z-index:20;top:0;left:0;bottom:0;right:0;background-color:rgba(0,0,0,0.2)}@-webkit-keyframes weuiLoading{0%{transform:rotate3d(0,0,1,0)}100%{transform:rotate3d(0,0,1,360deg)}}@keyframes weuiLoading{0%{transform:rotate3d(0,0,1,0)}100%{transform:rotate3d(0,0,1,360deg)}}@-webkit-keyframes slidein{0%{transform:translateX(-50%)}100%{transform:translateX(0)}}@keyframes slidein{0%{transform:translateX(-50%)}100%{transform:translateX(0)}}.mpda_bottom_container{position:relative}.rich_media_tool{overflow:hidden;line-height:32px}.rich_media_tool .meta_primary{float:left}.rich_media_tool .meta_extra{float:right}.rich_media_tool .meta_praise{text-align:right}.media_tool_meta i{vertical-align:0;position:relative;top:1px}.meta_praise{-webkit-tap-highlight-color:rgba(0,0,0,0);outline:0}.meta_praise .praise_num{display:inline-block;vertical-align:top}.meta_praise:hover{cursor:pointer}.icon_praise_gray{background:url("") 0 0 / 100% no-repeat transparent;display:inline-block}.rich_media_tool{font-size:15px}.rich_media_tool .meta_primary{margin-right:16px}.rich_media_tool .meta_extra{margin-left:16px;color:#576b95}.rich_media_tool .meta_praise{min-width:2.5em}.rich_media_tool .meta_praise i{margin-right:5px}.icon_praise_gray{background-image:url("data:image/svg+xml;charset=utf8, %3Csvg width='16' height='16' viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M2.5 6.988h-.003c-.095-.01-.167-.022-.125-.022H1.75c-.343 0-.75.39-.75.7v6.73c0 .31.27.57.611.57H2.5V7.01a.51.51 0 0 1 0-.022zm1 .003a.55.55 0 0 1 0 .02v7.955h7.414c.748 0 1.395-.361 1.773-1.324a37.17 37.17 0 0 0 1.115-2.57c.219-.564.413-1.11.575-1.627.247-.785.413-1.48.484-2.058.073-.595-.565-1.021-1.236-1.021h-4.97l.102-.586.18-1.027.13-.55a35.058 35.058 0 0 0 .245-1.128c.212-1.098-.483-2.019-1.238-2.067-.74-.048-1.1.111-1.104.562-.008 1.276-.45 2.805-1.252 4.129-.357.589-.899.965-1.56 1.16-.217.065-.438.107-.658.132zm6.345-1.625h3.78c1.19 0 2.393.804 2.229 2.143-.08.646-.26 1.397-.523 2.235-.17.54-.37 1.107-.597 1.69a38.158 38.158 0 0 1-1.133 2.61c-.525 1.346-1.557 1.922-2.687 1.922H1.61c-.886 0-1.611-.698-1.611-1.57v-6.73c0-.871.864-1.7 1.75-1.7l.719.009A3.285 3.285 0 0 0 3.876 5.9c.435-.13.769-.361.986-.72.71-1.171 1.102-2.525 1.108-3.618C5.978.338 6.901-.07 8.14.01c1.36.088 2.48 1.57 2.155 3.255a36.012 36.012 0 0 1-.253 1.167l-.124.52-.072.414z' fill='%23576B95' fill-rule='nonzero'/%3E%3C/svg%3E");font-size:16px;width:1em;height:1em;background-size:1em}.praise_num{color:#576b95}a,button{cursor:pointer}.rich_media_extra{overflow:hidden}.rich_media_extra_discuss{padding-top:0}.praise_num:empty{margin-left:-3px}.comment_primary_emotion_panel_wrp{position:absolute;z-index:1;padding-top:8px;padding-bottom:16px}.comment_primary_emotion_panel{background:#fff;box-shadow:rgba(0,0,0,0.16) 0 2px 8px 0;border-radius:4px;width:376px;height:216px;overflow-y:auto}.tips_global_primary{color:rgba(0,0,0,0.3)}.weui-dialog{position:fixed;z-index:5000;top:50%;left:16px;right:16px;transform:translate(0,-50%);background-color:#fff;text-align:center;border-radius:12px;overflow:hidden;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;flex-direction:column;max-height:90%}@media screen and (min-width:352px){.weui-dialog{width:320px;margin:0 auto}}.weui-toast{position:fixed;z-index:5000;width:120px;height:120px;top:40%;left:50%;transform:translate(-50%,-50%);background:rgba(17,17,17,0.7);text-align:center;border-radius:5px;color:#fff;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;flex-direction:column;-webkit-box-align:center;align-items:center;-webkit-box-pack:center;justify-content:center}.weui-mask{position:fixed;z-index:1000;top:0;right:0;left:0;bottom:0;background:rgba(0,0,0,0.6)}.weui-mask_transparent{position:fixed;z-index:1000;top:0;right:0;left:0;bottom:0}@-webkit-keyframes weuiLoading{0%{transform:rotate3d(0,0,1,0)}100%{transform:rotate3d(0,0,1,360deg)}}@keyframes weuiLoading{0%{transform:rotate3d(0,0,1,0)}100%{transform:rotate3d(0,0,1,360deg)}}@media screen and (max-width:1023px){.profile_container{display:none !important}}.weui-desktop-popover{white-space:normal;overflow-wrap:break-word;hyphens:auto;z-index:500;color:#353535;line-height:1.6;background:#fff;border-radius:2px}.weui-desktop-popover::before{content:" ";width:8px;height:8px;background-color:#fff;box-shadow:#d4d4d4 0 2px 10px 0;transform:matrix(0.71,0.71,-0.71,0.71,0,0);position:absolute}.weui-desktop-popover::after{content:" ";background-color:#fff;position:absolute}.weui-desktop-popover_img-text{text-align:center}.weui-desktop-popover_pos-up-center{margin-top:16px}.weui-desktop-popover_pos-up-left::before,.weui-desktop-popover_pos-up-center::before,.weui-desktop-popover_pos-up-right::before{top:-4px}.weui-desktop-popover_pos-up-left::after,.weui-desktop-popover_pos-up-center::after,.weui-desktop-popover_pos-up-right::after{height:10px;top:0;left:0;right:0}.weui-desktop-popover_pos-up-center::before,.weui-desktop-popover_pos-down-center::before{margin-left:-4px}.weui-desktop-popover{position:absolute;padding:14px;box-shadow:none;border:1px solid #d9dadc;width:182px;box-sizing:border-box}.weui-desktop-popover::before{box-shadow:none;border:1px solid #d9dadc}.not_in_mm .rich_media_meta_list{position:relative;z-index:1}.not_in_mm .rich_media_content{position:relative}.not_in_mm .profile_container{width:535px;position:absolute;top:100%;left:0;margin-top:10px;font-size:14px}.not_in_mm .profile_inner{position:relative;padding:30px 22px 36px 144px;background-color:#fff;border:1px solid #d9dadc}.not_in_mm .profile_arrow_wrp{position:absolute;left:22px;top:-8px}.not_in_mm .rich_media_inner{position:relative}.not_in_mm .qr_code_pc_outer{position:fixed;left:0;right:0;top:20px;color:#717375;text-align:center;display:none !important}.not_in_mm .qr_code_pc_inner{position:relative;width:740px;margin-left:auto;margin-right:auto}.not_in_mm .qr_code_pc{position:absolute;right:-140px;top:0;width:140px;padding:16px;border:1px solid #d9dadc;background-color:#fff;overflow-wrap:break-word;word-break:break-all}.not_in_mm .qr_code_pc p{font-size:14px;line-height:20px}.not_in_mm .qr_code_pc_img{width:102px;height:102px}@media screen and (min-width:1024px){.not_in_mm .qr_code_pc_outer{top:32px;display:block !important}}.not_in_mm .qr_code_pc{box-sizing:border-box}</style><link rel="shortcut icon" type=image/x-icon href=""></head>
 <body id=activity-detail class="zh_CN mm_appmsg appmsg_skin_default appmsg_style_default not_in_mm">
 
 
 
<div id=js_article class=rich_media>
 
 <div id=js_top_ad_area class=top_banner></div>
 
 <div class=rich_media_inner>
 
 
 <div id=page-content class=rich_media_area_primary>
 <div class=rich_media_area_primary_inner>
 
 
 
 <div id=img-content>
 
 <h2 class=rich_media_title id=activity-name>
 
 
 
 Java 性能优化的 45 个细节
 </h2>
 <div id=meta_content class=rich_media_meta_list>
 
 <span class="rich_media_meta rich_media_meta_nickname" id=profileBt>
 <a href=https://mp.weixin.qq.com/s/3LA6RwfenjHWNUlZYQQOkg id=js_name>
 Java架构师之路 </a>
 <div id=js_profile_qrcode class=profile_container style=display:none>
 <div class=profile_inner>
 
 
 
 
 
 </div>
 <span class=profile_arrow_wrp id=js_profile_arrow_wrp>
 
 
 </span>
 </div>
 </span>
 <em id=publish_time class="rich_media_meta rich_media_meta_text">4月29日</em>
 </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 <div class=rich_media_content id=js_content>
 
 
 
 
 <p style=text-align:right><span style=color:#ab1942;font-size:14px><strong>作者 | 未知 来源网络</strong></span><br><p><span style=font-size:16px>在JAVA程序中，性能问题的大部分原因并不在于JAVA语言，而是程序本身。养成良好的编码习惯非常重要，能够显著地提升程序性能。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>1. 尽量在合适的场合使用单例</strong></span></p></section></section></section><p><span style=font-size:16px>使用单例可以减轻加载的负担，缩短加载的时间，提高加载的效率，但并不是所有地方都适用于单例，简单来说，单例主要适用于以下三个方面：</span><p><br><ol class=list-paddingleft-2><li><p style=margin-top:5px;margin-bottom:5px><span style=font-size:16px>控制资源的使用，通过线程同步来控制资源的并发访问；</span></p><li><p style=margin-top:5px;margin-bottom:5px><span style=font-size:16px>控制实例的产生，以达到节约资源的目的；</span></p><li><p style=margin-top:5px;margin-bottom:5px><span style=font-size:16px>控制数据共享，在不建立直接关联的条件下，让多个不相关的进程或线程之间实现通信。</span><br></p></ol><p><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>2. 尽量避免随意使用静态变量</strong></span></p></section></section></section><p><span style=font-size:16px>当某个对象被定义为static变量所引用，那么GC通常是不会回收这个对象所占有的内存，如</span><p><br><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>class</span> A{</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>private</span> <span style=font-size:16px;color:#c678dd>static</span> B b = <span style=font-size:16px;color:#c678dd>new</span> B();</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>此时静态变量b的生命周期与A类同步，如果A类不会卸载，那么b对象会常驻内存，直到程序终止。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>3. 尽量避免过多过常地创建Java对象</strong></span></p></section></section></section><p><span style=font-size:16px>尽量避免在经常调用的方法，循环中new对象，由于系统不仅要花费时间来创建对象，而且还要花时间对这些对象进行垃圾回收和处理，在我们可以控制的范围内，最大限度地重用对象，最好能用基本的数据类型或数组来替代对象。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>4. 尽量使用final修饰符</strong></span></p></section></section></section><p><span style=font-size:16px>带有final修饰符的类是不可派生的。在JAVA核心API中，有许多应用final的例子，例如java、lang、String，为String类指定final防止了使用者覆盖length()方法。另外，如果一个类是final的，则该类所有方法都是final的。java编译器会寻找机会内联（inline）所有的final方法（这和具体的编译器实现有关），此举能够使性能平均提高50%。</span><p><br><p><span style=font-size:16px>如：让访问实例内变量的getter/setter方法变成”final：</span><p><br><p><span style=font-size:16px>简单的getter/setter方法应该被置成final，这会告诉编译器，这个方法不会被重载，所以，可以变成”inlined”,例子：</span><p><br><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>class</span> MAF {</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>setSize</span> (<span style=font-size:16px;color:#c678dd>int</span> size) {</span><br><br><span style=font-size:16px>_size = size;</span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>private</span> <span style=font-size:16px;color:#c678dd>int</span> _size;</span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px>更正</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>class</span> DAF_fixed {</span><br><br><span style=font-size:16px>final <span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>setSize</span> (<span style=font-size:16px;color:#c678dd>int</span> size) {</span><br><br><span style=font-size:16px>_size = size;</span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>private</span> <span style=font-size:16px;color:#c678dd>int</span> _size;</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>5. 尽量使用局部变量</strong></span></p></section></section></section><p><span style=font-size:16px>调用方法时传递的参数以及在调用中创建的临时变量都保存在栈（Stack）中，速度较快；其他变量，如静态变量、实例变量等，都在堆（Heap）中创建，速度较慢。</span><p><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>6. 尽量处理好包装类型和基本类型两者的使用场所</strong></span></p></section></section></section><p><span style=font-size:16px>虽然包装类型和基本类型在使用过程中是可以相互转换，但它们两者所产生的内存区域是完全不同的，基本类型数据产生和处理都在栈中处理，包装类型是对象，是在堆中产生实例。在集合类对象，有对象方面需要的处理适用包装类型，其他的处理提倡使用基本类型。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>7. 慎用synchronized，尽量减小synchronize的方法</strong></span></p></section></section></section><p><span style=font-size:16px>都知道，实现同步是要很大的系统开销作为代价的，甚至可能造成死锁，所以尽量避免无谓的同步控制。synchronize方法被调用时，直接会把当前对象锁了，在方法执行完之前其他线程无法调用当前对象的其他方法。所以，synchronize的方法尽量减小，并且应尽量使用方法同步代替代码块同步。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>8. 尽量不要使用finalize方法</strong></span></p></section></section></section><p><span style=font-size:16px>实际上，将资源清理放在finalize方法中完成是非常不好的选择，由于GC的工作量很大，尤其是回收Young代内存时，大都会引起应用程序暂停，所以再选择使用finalize方法进行资源清理，会导致GC负担更大，程序运行效率更差。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>9. 尽量使用基本数据类型代替对象</strong></span></p></section></section></section><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><span style=font-size:16px>String&nbsp;str&nbsp;=&nbsp;"hello";</span></pre><p><span style=letter-spacing:.7px;font-size:16px>上面这种方式会创建一个“hello”字符串，而且JVM的字符缓存池还会缓存这个字符串；</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><span style=font-size:16px>String&nbsp;str&nbsp;=&nbsp;new&nbsp;String("hello");</span></pre><p><span style=letter-spacing:.7px;font-size:16px>此时程序除创建字符串外，str所引用的String对象底层还包含一个char[]数组，这个char[]数组依次存放了h,e,l,l,o</span><p><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>10. 多线程在未发生线程安全前提下应尽量使用HashMap、ArrayList</strong></span></p></section></section></section><p><span style=font-size:16px>HashTable、Vector等使用了同步机制，降低了性能。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>11. 尽量合理的创建HashMap</strong></span></p></section></section></section><p><span style=font-size:16px>当你要创建一个比较大的hashMap时，充分利用这个构造函数</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><span style=font-size:16px>public&nbsp;HashMap(int&nbsp;initialCapacity,&nbsp;float&nbsp;loadFactor);</span></pre><p><span style=letter-spacing:.7px;font-size:16px>避免HashMap多次进行了hash重构,扩容是一件很耗费性能的事，在默认中initialCapacity只有16，而loadFactor是 0.75，需要多大的容量，你最好能准确的估计你所需要的最佳大小，同样的Hashtable，Vectors也是一样的道理。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>12. 量减少对变量的重复计算</strong></span></p></section></section></section><p><span style=font-size:16px>如：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><span style=font-size:16px>for(int&nbsp;i=0;i&lt;list.size();i++)</span></pre><p><br><p><span style=font-size:16px>应该改为：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><span style=font-size:16px>for(int&nbsp;i=0,len=list.size();i&lt;len;i++)</span></pre><p><br><p><span style=font-size:16px>并且在循环中应该避免使用复杂的表达式，在循环中，循环条件会被反复计算，如果不使用复杂表达式，而使循环条件值不变的话，程序将会运行的更快。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>13. 尽量避免不必要的创建</strong></span></p></section></section></section><p><span style=font-size:16px>如：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px>A a = <span style=font-size:16px;color:#c678dd>new</span> A();</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>if</span>(i==<span style=font-size:16px;color:#d19a66>1</span>){</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#e6c07b>list</span>.add(a);</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>应该改为：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>if</span>(i==<span style=font-size:16px;color:#d19a66>1</span>){</span><br><br><span style=font-size:16px>A a = <span style=font-size:16px;color:#c678dd>new</span> A();</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#e6c07b>list</span>.add(a);</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>14. 尽量在finally块中释放资源</strong></span></p></section></section></section><p><span style=font-size:16px>程序中使用到的资源应当被释放，以避免资源泄漏，这最好在finally块中去做。不管程序执行的结果如何，finally块总是会执行的，以确保资源的正确关闭。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>15. 尽量使用移位来代替'a/b'的操作</strong></span></p></section></section></section><p><span style=font-size:16px>"/"是一个代价很高的操作，使用移位的操作将会更快和更有效</span><p><span style=font-size:16px>如：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span> num = a / <span style=font-size:16px;color:#d19a66>4</span>;</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span> num = a / <span style=font-size:16px;color:#d19a66>8</span>;</span></code></pre><p><br><p><span style=font-size:16px>应该改为：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span> num = a >> <span style=font-size:16px;color:#d19a66>2</span>;</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span> num = a >> <span style=font-size:16px;color:#d19a66>3</span>;</span></code></pre><p><br><p><span style=font-size:16px>但注意的是使用移位应添加注释，因为移位操作不直观，比较难理解。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>16.尽量使用移位来代替'a*b'的操作</strong></span></p></section></section></section><p><span style=font-size:16px>同样的，对于'*'操作，使用移位的操作将会更快和更有效</span><p><br><p><span style=font-size:16px>如：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span> num = a * <span style=font-size:16px;color:#d19a66>4</span>;</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span> num = a * <span style=font-size:16px;color:#d19a66>8</span>;</span></code></pre><p><br><p><span style=font-size:16px>应该改为：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span> num = a &lt;&lt; <span style=font-size:16px;color:#d19a66>2</span>;</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span> num = a &lt;&lt; <span style=font-size:16px;color:#d19a66>3</span>;</span></code></pre><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>17. 尽量确定StringBuffer的容量</strong></span></p></section></section></section><p><span style=font-size:16px>StringBuffer 的构造器会创建一个默认大小（通常是16）的字符数组。在使用中，如果超出这个大小，就会重新分配内存，创建一个更大的数组，并将原先的数组复制过来，再丢弃旧的数组。在大多数情况下，你可以在创建 StringBuffer的时候指定大小，这样就避免了在容量不够的时候自动增长，以提高性能。</span><p><br><p><span style=font-size:16px>如：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><span style=font-size:16px>StringBuffer&nbsp;buffer&nbsp;=&nbsp;new&nbsp;StringBuffer(1000);</span></pre><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><strong><span style=font-size:16px>18. 尽量早释放无用对象的引用</span></strong></p></section></section></section><p><span style=font-size:16px>大部分时，方法局部引用变量所引用的对象会随着方法结束而变成垃圾，因此，大部分时候程序无需将局部，引用变量显式设为null。</span><p><br><p><span style=font-size:16px>例如：</span><p><span style=font-size:16px>Java代码</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px>Public <span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>test</span>(){</span><br><br><span style=font-size:16px>Object obj = <span style=font-size:16px;color:#c678dd>new</span> Object();</span><br><br><span style=font-size:16px>……</span><br><br><span style=font-size:16px>Obj=null;</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>上面这个就没必要了，随着方法test()的执行完成，程序中obj引用变量的作用域就结束了。但是如果是改成下面：</span><p><span style=font-size:16px>Java代码</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px>Public <span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>test</span>(){</span><br><br><span style=font-size:16px>Object obj = <span style=font-size:16px;color:#c678dd>new</span> Object();</span><br><br><span style=font-size:16px>……</span><br><br><span style=font-size:16px>Obj=null;</span><br><br><span style=color:#5c6370;font-style:italic;font-size:16px>//执行耗时，耗内存操作；或调用耗时，耗内存的方法</span><br><br><span style=font-size:16px>……</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>这时候就有必要将obj赋值为null，可以尽早的释放对Object对象的引用。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>19. 尽量避免使用二维数组</strong></span></p></section></section></section><p><span style=font-size:16px>二维数据占用的内存空间比一维数组多得多，大概10倍以上。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>20. 尽量避免使用split</strong></span></p></section></section></section><p><span style=font-size:16px>除非是必须的，否则应该避免使用split，split由于支持正则表达式，所以效率比较低，如果是频繁的几十，几百万的调用将会耗费大量资源，如果确实需要频繁的调用split，可以考虑使用apache的StringUtils.split(string,char)，频繁split的可以缓存结果。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>21. ArrayList &amp; LinkedList</strong></span></p></section></section></section><p><span style=font-size:16px>一个是线性表，一个是链表，一句话，随机查询尽量使用ArrayList，ArrayList优于LinkedList，LinkedList还要移动指针，添加删除的操作LinkedList优于ArrayList，ArrayList还要移动数据，不过这是理论性分析，事实未必如此，重要的是理解好2者得数据结构，对症下药。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>22. 尽量使用System.arraycopy ()代替通过来循环复制数组</strong></span></p></section></section></section><p><span style=font-size:16px>System.arraycopy() 要比通过循环来复制数组快的多。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>23. 尽量缓存经常使用的对象</strong></span></p></section></section></section><p><span style=font-size:16px>尽可能将经常使用的对象进行缓存，可以使用数组，或HashMap的容器来进行缓存，但这种方式可能导致系统占用过多的缓存，性能下降，推荐可以使用一些第三方的开源工具，如EhCache，Oscache进行缓存，他们基本都实现了FIFO/FLU等缓存算法。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>24. 尽量避免非常大的内存分配</strong></span></p></section></section></section><p><span style=font-size:16px>有时候问题不是由当时的堆状态造成的，而是因为分配失败造成的。分配的内存块都必须是连续的，而随着堆越来越满，找到较大的连续块越来越困难。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>25. 慎用异常</strong></span></p></section></section></section><p><span style=font-size:16px>当创建一个异常时，需要收集一个栈跟踪(stack track)，这个栈跟踪用于描述异常是在何处创建的。构建这些栈跟踪时需要为运行时栈做一份快照，正是这一部分开销很大。当需要创建一个 Exception 时，JVM 不得不说：先别动，我想就您现在的样子存一份快照，所以暂时停止入栈和出栈操作。栈跟踪不只包含运行时栈中的一两个元素，而是包含这个栈中的每一个元素。</span><p><br><p><span style=font-size:16px>如果您创建一个 Exception ，就得付出代价，好在捕获异常开销不大，因此可以使用 try-catch 将核心内容包起来。从技术上讲，你甚至可以随意地抛出异常，而不用花费很大的代价。招致性能损失的并不是 throw 操作——尽管在没有预先创建异常的情况下就抛出异常是有点不寻常。真正要花代价的是创建异常，幸运的是，好的编程习惯已教会我们，不应该不管三七二十一就抛出异常。异常是为异常的情况而设计的，使用时也应该牢记这一原则。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>26. 尽量重用对象</strong></span></p></section></section></section><p><span style=font-size:16px>特别是String对象的使用中，出现字符串连接情况时应使用StringBuffer代替，由于系统不仅要花时间生成对象，以后可能还需要花时间对这些对象进行垃圾回收和处理。因此生成过多的对象将会给程序的性能带来很大的影响。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>27. 不要重复初始化变量</strong></span></p></section></section></section><p><span style=font-size:16px>默认情况下，调用类的构造函数时，java会把变量初始化成确定的值，所有的对象被设置成null，整数变量设置成0，float和double变量设置成0.0，逻辑值设置成false。当一个类从另一个类派生时，这一点尤其应该注意，因为用new关键字创建一个对象时，构造函数链中的所有构造函数都会被自动调用。</span><p><br><p><span style=font-size:16px>这里有个注意，给成员变量设置初始值但需要调用其他方法的时候，最好放在一个方法。比如initXXX()中，因为直接调用某方法赋值可能会因为类尚未初始化而抛空指针异常，如：public int state = this.getState()。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>28.</strong></span></p></section></section></section><p><span style=font-size:16px>在java+Oracle的应用系统开发中，java中内嵌的SQL语言应尽量使用大写形式，以减少Oracle解析器的解析负担。</span><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>29.</strong></span></p></section></section></section><p><span style=letter-spacing:.7px;font-size:16px>在java编程过程中，进行数据库连接，I/O流操作，在使用完毕后，及时关闭以释放资源。因为对这些大对象的操作会造成系统大的开销。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>30.</strong></span></p></section></section></section><p><span style=font-size:16px>过分的创建对象会消耗系统的大量内存，严重时，会导致内存泄漏，因此，保证过期的对象的及时回收具有重要意义。JVM的GC并非十分智能，因此建议在对象使用完毕后，手动设置成null。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>31.</strong></span></p></section></section></section><p><span style=color:#000000;font-size:16px>在使用同步机制时，应尽量使用方法同步代替代码块同步。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>32.</strong></span></p></section></section></section><p><span style=color:#000000;font-size:16px>不要在循环中使用Try/Catch语句，应把Try/Catch放在循环最外层</span><br><p><br><p><span style=font-size:16px>Error是获取系统错误的类，或者说是虚拟机错误的类。不是所有的错误Exception都能获取到的，虚拟机报错Exception就获取不到，必须用Error获取。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>33. 通过StringBuffer的构造函数来设定它的初始化容量，可以明显提升性能</strong></span></p></section></section></section><p><span style=font-size:16px>StringBuffer的默认容量为16，当StringBuffer的容量达到最大容量时，它会将自身容量增加到当前的2倍+2，也就是2*n+2。无论何时，只要StringBuffer到达它的最大容量，它就不得不创建一个新的对象数组，然后复制旧的对象数组，这会浪费很多时间。所以给StringBuffer设置一个合理的初始化容量值，是很有必要的！</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>34. 合理使用java.util.Vector</strong></span></p></section></section></section><p><span style=font-size:16px>Vector与StringBuffer类似，每次扩展容量时，所有现有元素都要赋值到新的存储空间中。Vector的默认存储能力为10个元素，扩容加倍。</span><p><br><p><span style=font-size:16px>vector.add(index,obj) 这个方法可以将元素obj插入到index位置，但index以及之后的元素依次都要向下移动一个位置（将其索引加 1）。除非必要，否则对性能不利。同样规则适用于remove(int index)方法，移除此向量中指定位置的元素。将所有后续元素左移（将其索引减 1）。返回此向量中移除的元素。所以删除vector最后一个元素要比删除第1个元素开销低很多。删除所有元素最好用removeAllElements()方法。</span><p><br><p><span style=font-size:16px>如果要删除vector里的一个元素可以使用 vector.remove(obj)；而不必自己检索元素位置，再删除，如int index = indexOf（obj）;vector.remove(index)。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>35. 不用new关键字创建对象的实例</strong></span></p></section></section></section><p><span style=font-size:16px>用new关键词创建类的实例时，构造函数链中的所有构造函数都会被自动调用。但如果一个对象实现了Cloneable接口，我们可以调用它的clone()方法。clone()方法不会调用任何类构造函数。</span><p><br><p><span style=font-size:16px>下面是Factory模式的一个典型实现：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>static</span> Credit <span style=font-size:16px;color:#61aeee>getNewCredit</span>()<br><br>{</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>return</span> <span style=font-size:16px;color:#c678dd>new</span> Credit();</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=color:#222222;font-size:16px>改进后的代码使用clone()方法：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>private</span> <span style=font-size:16px;color:#c678dd>static</span> Credit BaseCredit = <span style=font-size:16px;color:#c678dd>new</span> Credit();</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>static</span> Credit <span style=font-size:16px;color:#61aeee>getNewCredit</span>()<br><br>{</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>return</span> (Credit)BaseCredit.clone();</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>36.</strong></span></p></section></section></section><p><span style=color:#000000;font-size:16px>不要将数组声明为：public static final</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>37. HaspMap的遍历：</strong></span></p></section></section></section><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px>Map&lt;String, String[]> paraMap = <span style=font-size:16px;color:#c678dd>new</span> HashMap&lt;String, String[]>();</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>for</span>( Entry&lt;String, String[]> entry : paraMap.entrySet() )</span><br><br><span style=font-size:16px>{</span><br><br><span style=font-size:16px>String appFieldDefId = entry.getKey();</span><br><br><span style=font-size:16px>String[] values = entry.getValue();</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>利用散列值取出相应的Entry做比较得到结果，取得entry的值之后直接取key和value。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>38. array(数组)和ArrayList的使用</strong></span></p></section></section></section><p><span style=font-size:16px>array 数组效率最高，但容量固定，无法动态改变，ArrayList容量可以动态增长，但牺牲了</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>39.</strong></span></p></section></section></section><p><span style=font-size:16px>单线程应尽量使用 HashMap, ArrayList,除非必要，否则不推荐使用HashTable,Vector，它们使用了同步机制，而降低了性能。</span><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>40.</strong></span></p></section></section></section><p><span style=font-size:16px>StringBuffer,StringBuilder的区别在于：java.lang.StringBuffer 线程安全的可变字符序列。一个类似于String的字符串缓冲区，但不能修改。StringBuilder与该类相比，通常应该优先使用StringBuilder类，因为它支持所有相同的操作，但由于它不执行同步，所以速度更快。为了获得更好的性能，在构造StringBuffer或StringBuilder时应尽量指定她的容量。当然如果不超过16个字符时就不用了。相同情况下，使用StringBuilder比使用StringBuffer仅能获得10%~15%的性能提升，但却要冒多线程不安全的风险。综合考虑还是建议使用StringBuffer。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>41.</strong></span></p></section></section></section><p><span style=color:#000000;font-size:16px>尽量使用基本数据类型代替对象。</span><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>42.</strong></span></p></section></section></section><p><span style=color:#000000;font-size:16px>使用具体类比使用接口效率高，但结构弹性降低了，但现代IDE都可以解决这个问题。</span><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>43.</strong></span></p></section></section></section><p><span style=font-size:16px>考虑使用静态方法，如果你没有必要去访问对象的外部，那么就使你的方法成为静态方法。它会被更快地调用，因为它不需要一个虚拟函数导向表。这同时也是一个很好的实践，因为它告诉你如何区分方法的性质，调用这个方法不会改变对象的状态。</span><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>44.</strong></span></p></section></section></section><p><span style=color:#000000;font-size:16px>应尽可能避免使用内在的GET,SET方法。</span><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>45.</strong></span></p></section></section></section><p><span style=color:#000000;font-size:16px>避免枚举，浮点数的使用。<br></span><p><br><p style=text-align:center><span style=color:#ab1942;font-size:18px><strong>以下举几个实用优化的例子：</strong></span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>一、避免在循环条件中使用复杂表达式</strong></span></p></section></section></section><p><span style=font-size:16px>在不做编译优化的情况下，在循环中，循环条件会被反复计算，如果不使用复杂表达式，而使循环条件值不变的话，程序将会运行的更快。例子：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>import</span> java.util.Vector;</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>class</span> CEL {</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>method</span> (Vector <span style=font-size:16px;color:#e6c07b>vector</span>) {</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>for</span> (<span style=font-size:16px;color:#c678dd>int</span> i = <span style=font-size:16px;color:#d19a66>0</span>; i &lt; <span style=font-size:16px;color:#e6c07b>vector</span>.size (); i++) <span style=font-size:16px;color:#5c6370;font-style:italic>// Violation</span></span><br><br><span style=font-size:16px>; <span style=font-size:16px;color:#5c6370;font-style:italic>// ...</span></span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>更正：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>class</span> CEL_fixed {</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>method</span> (Vector <span style=font-size:16px;color:#e6c07b>vector</span>) {</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span> size = <span style=font-size:16px;color:#e6c07b>vector</span>.size ()</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>for</span> (<span style=font-size:16px;color:#c678dd>int</span> i = <span style=font-size:16px;color:#d19a66>0</span>; i &lt; size; i++)</span><br><br><span style=font-size:16px>; <span style=font-size:16px;color:#5c6370;font-style:italic>// ...</span></span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>二、为'Vectors' 和 'Hashtables'定义初始大小</strong></span></p></section></section></section><p><span style=font-size:16px>JVM为Vector扩充大小的时候需要重新创建一个更大的数组，将原原先数组中的内容复制过来，最后，原先的数组再被回收。可见Vector容量的扩大是一个颇费时间的事。</span><p><br><p><span style=font-size:16px>通常，默认的10个元素大小是不够的。你最好能准确的估计你所需要的最佳大小。例子：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>import</span> java.util.Vector;</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>class</span> DIC {</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>addObjects</span> (Object[] o) {</span><br><br><span style=color:#5c6370;font-style:italic;font-size:16px>// if length > 10, Vector needs to expand</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>for</span> (<span style=font-size:16px;color:#c678dd>int</span> i = <span style=font-size:16px;color:#d19a66>0</span>; i&lt; o.length;i++) {</span><br><br><span style=font-size:16px>v.add(o); <span style=font-size:16px;color:#5c6370;font-style:italic>// capacity before it can add more elements.</span></span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> Vector v = <span style=font-size:16px;color:#c678dd>new</span> Vector(); <span style=font-size:16px;color:#5c6370;font-style:italic>// no initialCapacity.</span></span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>更正：</span><p><span style=font-size:16px>自己设定初始大小。</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> Vector v = <span style=font-size:16px;color:#c678dd>new</span> Vector(<span style=font-size:16px;color:#d19a66>20</span>);</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> Hashtable hash = <span style=font-size:16px;color:#c678dd>new</span> Hashtable(<span style=font-size:16px;color:#d19a66>10</span>);</span></code></pre><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>三、在finally块中关闭Stream</strong></span></p></section></section></section><p><span style=font-size:16px>程序中使用到的资源应当被释放，以避免资源泄漏。这最好在finally块中去做。不管程序执行的结果如何，finally块总是会执行的，以确保资源的正确关闭。</span><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>四、使用'System.arraycopy ()'代替通过来循环复制数组</strong></span></p></section></section></section><p><span style=font-size:16px>例子：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>class</span> IRB</span><br><br><span style=font-size:16px>{</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>method</span> () {</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span>[] array1 = <span style=font-size:16px;color:#c678dd>new</span> <span style=font-size:16px;color:#c678dd>int</span> [<span style=font-size:16px;color:#d19a66>100</span>];</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>for</span> (<span style=font-size:16px;color:#c678dd>int</span> i = <span style=font-size:16px;color:#d19a66>0</span>; i &lt; array1.length; i++) {</span><br><br><span style=font-size:16px>array1 [i] = i;</span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span>[] array2 = <span style=font-size:16px;color:#c678dd>new</span> <span style=font-size:16px;color:#c678dd>int</span> [<span style=font-size:16px;color:#d19a66>100</span>];</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>for</span> (<span style=font-size:16px;color:#c678dd>int</span> i = <span style=font-size:16px;color:#d19a66>0</span>; i &lt; array2.length; i++) {</span><br><br><span style=font-size:16px>array2 [i] = array1 [i]; <span style=font-size:16px;color:#5c6370;font-style:italic>// Violation</span></span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>更正：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>class</span> IRB</span><br><br><span style=font-size:16px>{</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>method</span> () {</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span>[] array1 = <span style=font-size:16px;color:#c678dd>new</span> <span style=font-size:16px;color:#c678dd>int</span> [<span style=font-size:16px;color:#d19a66>100</span>];</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>for</span> (<span style=font-size:16px;color:#c678dd>int</span> i = <span style=font-size:16px;color:#d19a66>0</span>; i &lt; array1.length; i++) {</span><br><br><span style=font-size:16px>array1 [i] = i;</span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>int</span>[] array2 = <span style=font-size:16px;color:#c678dd>new</span> <span style=font-size:16px;color:#c678dd>int</span> [<span style=font-size:16px;color:#d19a66>100</span>];</span><br><br><span style=font-size:16px>System.arraycopy(array1, <span style=font-size:16px;color:#d19a66>0</span>, array2, <span style=font-size:16px;color:#d19a66>0</span>, <span style=font-size:16px;color:#d19a66>100</span>);</span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong><br></strong></span><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>五、让访问实例内变量的getter/setter方法变成”final”</strong></span></p></section></section></section><p><span style=font-size:16px>简单的getter/setter方法应该被置成final，这会告诉编译器，这个方法不会被重载，所以，可以变成”inlined”,例子：</span><p><br><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>class</span> MAF {</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>setSize</span> (<span style=font-size:16px;color:#c678dd>int</span> size) {</span><br><br><span style=font-size:16px>_size = size;</span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>private</span> <span style=font-size:16px;color:#c678dd>int</span> _size;</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>更正：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>class</span> DAF_fixed {</span><br><br><span style=font-size:16px>final <span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>setSize</span> (<span style=font-size:16px;color:#c678dd>int</span> size) {</span><br><br><span style=font-size:16px>_size = size;</span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>private</span> <span style=font-size:16px;color:#c678dd>int</span> _size;</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>六、对于常量字符串，用'String' 代替 'StringBuffer'</strong></span></p></section></section></section><p><span style=font-size:16px>常量字符串并不需要动态改变长度。</span><p><br><p><span style=font-size:16px>例子：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>class</span> USC {</span><br><br><span style=font-size:16px>String <span style=font-size:16px;color:#61aeee>method</span> () {</span><br><br><span style=font-size:16px>StringBuffer s = <span style=font-size:16px;color:#c678dd>new</span> StringBuffer (<span style=font-size:16px;color:#98c379>"Hello"</span>);</span><br><br><span style=font-size:16px>String t = s + <span style=font-size:16px;color:#98c379>"World!"</span>;</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>return</span> t;</span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>更正：把StringBuffer换成String，如果确定这个String不会再变的话，这将会减少运行开销提高性能。</span><p><br><p><br><section><section style="margin-right:auto;margin-bottom:10px;margin-left:auto;border-bottom:1px solid #dddddd"><section style="margin-bottom:-1px;padding-right:5px;padding-bottom:6px;padding-left:5px;clear:both;border-bottom:2px solid #ef7060;display:inline-block;line-height:1.1"><p style=color:#000000;font-size:15px><span style=font-size:16px><strong>七、在字符串相加的时候，使用 ' ' 代替 " "，如果该字符串只有一个字符的话</strong></span></p></section></section></section><p><span style=font-size:16px>例子：</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>class</span> STR {</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>method</span>(String s) {</span><br><br><span style=font-size:16px>String <span style=font-size:16px;color:#e6c07b>string</span> = s + <span style=font-size:16px;color:#98c379>"d"</span> <span style=font-size:16px;color:#5c6370;font-style:italic>// violation.</span></span><br><br><span style=font-size:16px><span style=font-size:16px;color:#e6c07b>string</span> = <span style=font-size:16px;color:#98c379>"abc"</span> + <span style=font-size:16px;color:#98c379>"d"</span> <span style=font-size:16px;color:#5c6370;font-style:italic>// violation.</span></span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>更正：</span><p><span style=font-size:16px>将一个字符的字符串替换成' '</span><pre style=text-align:start;background-color:#ffffff;color:#262626;font-size:14px;letter-spacing:.7px;overflow-x:auto><code style="margin-right:.15em;margin-left:.15em;padding:.5em;font-size:.85em;font-family:Consolas,Menlo,Courier,monospace;overflow:auto;color:#abb2bf;text-size-adjust:none;min-width:400px;background:none 0 0 / auto repeat scroll padding-box border-box #282c34;display:block !important"><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>class</span> STR {</span><br><br><span style=font-size:16px><span style=font-size:16px;color:#c678dd>public</span> <span style=font-size:16px;color:#c678dd>void</span> <span style=font-size:16px;color:#61aeee>method</span>(String s) {</span><br><br><span style=font-size:16px>String <span style=font-size:16px;color:#e6c07b>string</span> = s + <span style=font-size:16px;color:#98c379>'d'</span></span><br><br><span style=font-size:16px><span style=font-size:16px;color:#e6c07b>string</span> = <span style=font-size:16px;color:#98c379>"abc"</span> + <span style=font-size:16px;color:#98c379>'d'</span></span><br><br><span style=font-size:16px>}</span><br><br><span style=font-size:16px>}</span></code></pre><p><br><p><span style=font-size:16px>以上仅是Java方面编程时的性能优化，性能优化大部分都是在时间、效率、代码结构层次等方面的权衡，各有利弊，不要把上面内容当成教条，或许有些对我们实际工作适用，有些不适用，还望根据实际工作场景进行取舍，活学活用，变通为宜。</span><p style="margin:15px 8px;font-variant-numeric:normal;font-variant-east-asian:normal;white-space:normal;letter-spacing:.544px;widows:1;background-color:#ffffff;color:#3e3e3e;font-family:Helvetica,Arial,sans-serif;font-size:15px;line-height:1.75em;text-align:center"><strong><span style=letter-spacing:1px;color:#407600>-END-</span></strong><p style='margin:5px 8px;font-variant-numeric:normal;font-variant-east-asian:normal;white-space:normal;letter-spacing:.544px;font-size:16px;widows:1;background-color:#ffffff;font-family:-apple-system-font,system-ui,"Helvetica Neue","PingFang SC","Hiragino Sans GB","Microsoft YaHei UI","Microsoft YaHei",Arial,sans-serif;color:#3e3e3e;word-spacing:2px;text-align:center'><span style=color:#ffffff;background-color:#407600>&nbsp;</span><span style=color:#ffffff;font-size:15px;background-color:#407600>近期热文</span><p style=white-space:normal;letter-spacing:.544px;font-size:16px;background-color:#ffffff;text-align:center><a href="https://mp.weixin.qq.com/s?__biz=MzI3NjU2ODA5Mg==&amp;mid=2247484442&amp;idx=1&amp;sn=eede3d80743100b72e10003853356639&amp;scene=21#wechat_redirect" target=_blank data-linktype=2 style=-webkit-tap-highlight-color:rgba(0,0,0,0)><span style=-webkit-tap-highlight-color:rgba(0,0,0,0);color:#0080ff;text-decoration:underline;font-size:15px>详解设计模式在spring中的应用</span></a><p style=white-space:normal;letter-spacing:.544px;font-size:16px;background-color:#ffffff;text-align:center><a href="https://mp.weixin.qq.com/s?__biz=MzI3NjU2ODA5Mg==&amp;mid=2247484442&amp;idx=2&amp;sn=b55f26ca66a3a44a417a2839ef080672&amp;scene=21#wechat_redirect" target=_blank data-linktype=2 style=-webkit-tap-highlight-color:rgba(0,0,0,0)><span style=-webkit-tap-highlight-color:rgba(0,0,0,0);color:#0080ff;font-size:15px>Git最佳实践，基本操作和分支管理</span><br></a><p style=white-space:normal;letter-spacing:.544px;font-size:16px;background-color:#ffffff;text-align:center><a href="https://mp.weixin.qq.com/s?__biz=MzI3NjU2ODA5Mg==&amp;mid=2247484415&amp;idx=1&amp;sn=462d6a62b058a63d3db07948b9243e9c&amp;scene=21#wechat_redirect" target=_blank data-linktype=2 style=-webkit-tap-highlight-color:rgba(0,0,0,0)><span style=-webkit-tap-highlight-color:rgba(0,0,0,0);color:#0080ff;text-decoration:underline;font-size:15px>spring mvc工作原理</span></a><p style=white-space:normal;letter-spacing:.544px;background-color:#ffffff;text-align:center><a href="https://mp.weixin.qq.com/s?__biz=MzI3NjU2ODA5Mg==&amp;mid=2247484442&amp;idx=3&amp;sn=3cee71eb12a9e9ae6dfd4664df32b350&amp;scene=21#wechat_redirect" target=_blank data-linktype=2 style=color:#0080ff;text-decoration:underline;-webkit-tap-highlight-color:rgba(0,0,0,0);font-size:15px>初探Java字符串</a><p style=white-space:normal><img data-src="https://mmbiz.qpic.cn/mmbiz_jpg/M7B64fHXIStj2G1ryBlfibourJLibdiaATePdUl76gZQ2VEHHRIcLrKgYXCTOVc71xYXgS9QvmibTcnm56xDnKoBhQ/640?wx_fmt=jpeg" data-type=jpeg data-ratio=0.3333333333333333 data-w=1080 data-backw=556 data-backh=186 data-before-oversubscription-url="https://mmbiz.qpic.cn/mmbiz_jpg/M7B64fHXIStj2G1ryBlfibourJLibdiaATePdUl76gZQ2VEHHRIcLrKgYXCTOVc71xYXgS9QvmibTcnm56xDnKoBhQ/640?wx_fmt=jpeg" style="width:555.733px !important;height:auto !important;visibility:visible !important" _width=555.733px src="" crossorigin=anonymous data-fail=0></p>
 </div>
 
 
 <div class=ct_mpda_wrp id=js_sponsor_ad_area style=display:none></div>
 
 <div class=read-more__area id=js_more_read_area style=display:none>
 
 </div>
 </div>
 
 
 
 <ul id=js_hotspot_area class=article_extend_area></ul>
 
 
<div class=rich_media_tool id=js_toobar3>
 <div class=weui-flex>
 <div class=weui-flex__item>
 
 <div id=js_read_area3 class="media_tool_meta tips_global_primary meta_primary" style=display:none>
 <span id=readTxt>阅读</span>
 <span id=readNum3></span>
 </div>
 </div>
 <span style=display:none class="media_tool_meta meta_extra meta_praise" id=like_old>
 <i class=icon_praise_gray></i><span class=praise_num id=likeNum_old></span>
 </span>
 
 <span style=visibility:hidden class="media_tool_meta meta_extra meta_like" id=like3>
 <button class=like_btn id=js_like_btn> 
 <span id=js_like_wording> 在看</span><span class=like_num id=likeNum3></span>
 </button>
 </span>
 
 </div>
</div>
 
 <div class=like_comment_wrp id=js_like_comment style=display:none>
 <div class=like_comment_inner>
 
 
 </div>
 </div> 
 <div style=display:none id=wow_close_inform>
 <div class=weui-mask></div>
 <div class=weui-dialog>
 
 
 
 </div>
 </div>
<div id=js_like_toast style=display:none>
 <div class=weui-mask_transparent></div>
 <div class=weui-toast>
 
 
 </div>
</div>
<div style=display:none id=js_comment_panel>
 <div class="like_comment_primary_wrp editing" id=js_comment_wrp>
 
 </div> 
 <div class=like_comment_primary_mask id=js_mask_2></div>
</div>
<div id=js_loading style=display:none>
 <div class=weui-mask_transparent></div>
 <div class=weui-toast>
 
 
 </div>
</div>
 </div>
 </div>
 <div class="rich_media_area_primary sougou" id=sg_tj style=display:none></div>
 
 <div class=rich_media_area_extra>
 <div class=rich_media_area_extra_inner>
 
 <div id=js_share_appmsg>
 </div>
 
 
 <div class=mpda_bottom_container id=js_bottom_ad_area style=display:none></div>
 
 <div id=js_iframetest style=display:none></div>
 
 <div class="rich_media_extra rich_media_extra_discuss" id=js_cmt_container style=display:none>
 
 
 <div class=discuss_mod id=js_friend_cmt_area style=display:none>
 
 
 
 </div>
 <div class=discuss_mod id=js_cmt_area style=display:none>
 </div>
 </div>
 </div>
 </div>
 
 <div id=js_pc_qr_code class=qr_code_pc_outer style=display:block;>
 <div class=qr_code_pc_inner>
 <div class=qr_code_pc>
 <img id=js_pc_qr_code_img class=qr_code_pc_img src="">
 <p>微信扫一扫<br>关注该公众号</p>
 </div>
 </div>
 </div>
 </div>
</div>
<div id=js_pc_weapp_code class="weui-desktop-popover weui-desktop-popover_pos-up-center weui-desktop-popover_img-text" style=display:none>
 <div class=weui-desktop-popover__content>
 
 </div>
</div>
<div id=js_minipro_dialog style=display:none>
 <div class=weui-mask></div>
 <div class="weui-dialog weui-dialog_link">
 
 
 
 
 </div>
</div>
<div id=js_link_dialog style=display:none>
 <div class=weui-mask></div>
 <div class="weui-dialog weui-dialog_link">
 
 
 
 
 </div>
</div>
<div class=comment_primary_emotion_panel_wrp id=js_emotion_panel_pc style=display:none>
 <div class=comment_primary_emotion_panel>
 
 </div>
</div>
<div class=weui-dialog__wrp id=js_alert_panel style=display:none>
 <div class=weui-mask></div>
 <div class=weui-dialog>
 
 
 </div>
</div>
<div id=js_weapp_without_auth_dialog style=display:none>
 <div class=weui-mask></div>
 <div class="weui-dialog weui-dialog_link">
 
 
 </div>
</div>
 
 
 
 
 
 
